package im.device.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ExpandableListAdapter;
import android.widget.SimpleExpandableListAdapter;
import app.utils.SystemInfoUtils;

/**
 * 设备信息列表页面
 * @author EX-LIJINHUA001
 * @date 2013-3-7 
 */
public class DeviceInfoActivity extends ExpandableListActivity {
	
	/**
	 * TAG
	 */
	private static final String TAG = DeviceInfoActivity.class.getSimpleName();
	
    private static final String NAME = "NAME";
    private static final String IS_EVEN = "IS_EVEN";
    
    private ExpandableListAdapter mAdapter;
    
    private SystemInfoUtils mSystemInfoUtils;
    
    private List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
    private List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSystemInfoUtils = new SystemInfoUtils(this);
        
        init();
        
//        CommonUtils.saveSysMap(this, Constants.DEVICE_ID, "1");
//        String id = CommonUtils.getSysValue(this, Constants.DEVICE_ID,"0");
//        AppLog.d(TAG,"id = "+id);
        MessageService.startService(this.getApplicationContext());
//        MessageManager.getInstance(this).getPhotoList();
    }

    private void init(){
    	
        //=============================================start
        addGroup("TelephonyManager",mSystemInfoUtils.logTelephonyManager());
        addGroup("AndroidOsBuild",mSystemInfoUtils.logAndroidOsBuild());
        addGroup("CpuInfo",mSystemInfoUtils.logCpuInfo());
        addGroup("DiskInfo",mSystemInfoUtils.logDiskInfo());
        addGroup("DisplayMetrics",mSystemInfoUtils.logDisplayMetrics());
        addGroup("LinuxVersionInfo",mSystemInfoUtils.logLinuxVersionInfo());
        addGroup("MemoryInfo",mSystemInfoUtils.logMemoryInfo());
        addGroup("NetworkConfigInfo",mSystemInfoUtils.logNetworkConfigInfo());
        addGroup("SDCardSize",mSystemInfoUtils.logSDCardSize());
        addGroup("SystemProperty",mSystemInfoUtils.logSystemProperty());
        //=============================================end
        
        SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        HashMap<String,String> sensorMap = new HashMap<String,String>();
        List<Sensor> list = sm.getSensorList(Sensor.TYPE_ALL);
        for(Sensor s:list){
        	sensorMap.put(s.getName(),"true");
        }
        addGroup("Sensor",sensorMap);
        
        // Set up our adapter
        mAdapter = new SimpleExpandableListAdapter(
                this,
                groupData,
                R.layout.list_item_group,
                new String[] { NAME },
                new int[] { R.id.text1,},
                childData,
                R.layout.list_item_child,
                new String[] { NAME, IS_EVEN },
                new int[] { R.id.text1,R.id.text2 }
                );
        setListAdapter(mAdapter);
    }
    
    /**
     * 增加一组数据
     * @author EX-LIJINHUA001
     * @date 2013-3-7
     * @param name
     * @param data
     */
    private void addGroup(String name,HashMap<String,String> data){
    	HashMap<String,String> map = data;
        Map<String, String> curGroupMap = new HashMap<String, String>();
        groupData.add(curGroupMap);
        curGroupMap.put(NAME, name);
        List<Map<String, String>> children = new ArrayList<Map<String, String>>();
        Set<String> set = map.keySet();
        for(String key : set){
        	Map<String, String> curChildMap = new HashMap<String, String>();
        	children.add(curChildMap);
        	String value = map.get(key);
        	curChildMap.put(NAME, key);
            curChildMap.put(IS_EVEN, value);
        }
        childData.add(children);
    }
}